package com.aiju.cash.ui.member.adater;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.aiju.cash.R;
import com.aiju.cash.ui.member.entity.MemberCard;
import com.aiju.utils.PriceUtil;

import java.util.List;

/**
 * @author: 佐为
 * @date: 2017-02-22 09:40
 * @version: V1.0
 * @Description: 会员信息，已领会员卡列表
 */
public class MemberInfoCardAdapter extends BaseAdapter {

    private boolean select;
    private Context context;
    private List<MemberCard> memberCards;

    public MemberInfoCardAdapter(Context context, List<MemberCard> memberCards) {
        this.context = context;
        this.memberCards = memberCards;
    }

    public void setSelect(boolean select) {
        this.select = select;
    }

    @Override
    public int getCount() {
        return memberCards.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_member_info_card, null);
            holder = new ViewHolder();
            holder.nameView = (TextView) convertView.findViewById(R.id.tv_card_name);
            holder.stateView = (TextView) convertView.findViewById(R.id.tv_card_time);
            holder.codeView = (TextView) convertView.findViewById(R.id.tv_card_number);
            holder.discontView = (TextView) convertView.findViewById(R.id.tv_card_discoun);
            holder.selectView = convertView.findViewById(R.id.iv_member_select);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        MemberCard card = memberCards.get(position);

        holder.selectView.setVisibility(select ? View.VISIBLE : View.GONE);
        if (select) {
            holder.selectView.setSelected(card.isSelect() ? true : false);
        }
        holder.nameView.setText(card.getName());
        holder.codeView.setText(card.getCode());
        if (card.isForever()) {
            holder.stateView.setText("无限期");
        } else {
            holder.stateView.setText(card.getTime());
        }

        holder.discontView.setText(PriceUtil.formatDiscount(card.getDiscount()) + "折");
        return convertView;
    }

    public class ViewHolder {
        public View selectView;
        public TextView nameView;
        public TextView codeView;
        public TextView stateView;
        public TextView discontView;
    }

}
